
<!doctype html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/navbar-static-top/">

    <title>User</title>

    <!-- Bootstrap core CSS -->
    <link href="/css/bootstrap.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/examples/navbar-static-top/navbar-static-top.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div id="app">
<!-- Static navbar -->
<nav class="navbar navbar-default navbar-static-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">看板</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#">首页</a></li>
                <li><a href="#">关于</a></li>
                <li><a href="#">联系我们</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">工作流 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">待办故事</a></li>
                        <li><a href="#">迭代任务</a></li>
                        <li><a href="#">后端开发</a></li>
                        <li role="separator" class="divider"></li>
                        <li class="dropdown-header">前端开发</li>
                        <li><a href="#">软件测试</a></li>
                        <li><a href="#">运行维护</a></li>
                    </ul>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <div>
                    <a href="#">{{"用户:<"+islogin.name+">已登录"}}</a>
                    <a href="/login.html"><button class="btn btn-danger" @click="loginT">登出</button></a>
                </div>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>


<div class="container-fluid">

    <div class="row">
        <div class="col-md-4">
            <div class="panel panel-success">
                <!-- Default panel contents -->
                <div class="panel-heading">功能操作区</div>
                <div class="panel-body">
                    <div class="list-group">
                        <a href="/sub/flow/flow.html" class="list-group-item ">
                            工作流管理
                        </a>
                        <a href="/sub/story/story.html" class="list-group-item">故事管理</a>
                        <a href="/sub/task/task.html" class="list-group-item">任务管理</a>
                        <a href="/sub/user/user.html" class="list-group-item active">用户管理</a>
                        <a href="/sub/kanban/kanban.html" class="list-group-item">看板墙</a>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-8">
    <div class="panel panel-success">
        <!-- Default panel contents -->
        <div class="panel-heading">用户信息列表</div>
        <div class="panel-body">
            <p>以下对用户信息进行CRUD</p>
        </div>

        <!--        登录功能-->
        <table>
            <input type="text" name="username" class="form-control"
                   v-model="editUser.username" placeholder="用户账号" required="" autofocus="">
            <input type="password" name="password" class="form-control"
                   v-model="editUser.password" placeholder="账号密码" required="">
            <!--            <input type="submit" class="btn btn-info btn-sm" value="登录" onclick="app.login()">-->
        </table>
        <button type="submit" class="btn btn-info btn-sm" @click="login">登录</button>

<!--        头像提交-->
        <form action="/user/upload" method="post" target="yb" enctype="multipart/form-data">
            <input v-model="userheadimg" type="file" name="file" /><br/>
            <input type="submit" value="提交头像" />
        </form>
<!--        在from表单中使用<iframe>伪异步提交-->
        <iframe name="yb" id="yb" style="display: none"></iframe>
        <button @click="headimgT">刷新头像</button>
<!--        显示头像-->
        <div>
            <p>用户头像为:</p>
            <img :src="islogin.headimg" style="height: 500px; width: 500px"/>
        </div>


        <!-- Table 添加用户功能-->
        <table>
            <td><label>用户姓名</label><input type="text" class="form-control" v-model="editUser.name"></td>
            <td><label>用户账号</label><input type="text" class="form-control" v-model="editUser.username"></td>
            <td><label>用户密码</label><input type="password" class="form-control" v-model="editUser.password"></td>
            <td><label>用户工号</label><input type="text" class="form-control" v-model="editUser.jobno"></td>
            <td><label>用户职位</label><input type="text" class="form-control" v-model="editUser.position"></td>
            </tr>
        </table>
        <button class="btn btn-info btn-sm" @click="addUserByDefault">点击添加用户</button>
<!--        <a href="/user/save" class="btn btn-lg btn-primary">超链接添加</a>-->
<!--        <button class="btn btn-primary btn-sm" @click="addUserByDefault">Ajax 异步添加</button>-->



        <!--        根据用户姓名查询用户信息-->
        <table>
            <tr> <td>查询用户信息:</td><td>
                <label >
                    <input type="text" placeholder="请输入需要查询的用户姓名" id="user">
                    <input type="button" name="name" value="查询" onclick="app.findByUserName()">
                    <input type="button" name="all" value="查询全部用户" onclick="app.loadAllUser()">
                </label></td></tr>
        </table>

        <table class="table">
            <tr>
                <th>主键</th>
                <th>用户姓名</th>
                <th>用户账号</th>
                <th>用户密码</th>
                <th>用户工号</th>
                <th>用户职位</th>
                <th>操作</th>
            </tr>
            <tr v-for="(u,index) in users" :key="u.id" >
                <td>{{u.id}}</td>
                <td>{{u.name}}</td>
                <td>{{u.username}}</td>
                <td>{{u.password}}</td>
                <td>{{u.jobno}}</td>
                <td>{{u.position}}</td>
                <td>
                    <a :href="'/user/delete/'+u.id" class="btn btn-danger">超链接删除</a>
                    <button class="btn btn-warning btn-sm" @click="deleteUserById(u.id,index)">Ajax 异步删除</button>
                    <button class="btn btn-info btn-sm" @click="editUserById(u.id,index)">编辑</button>
                </td>
            </tr>
            <tr :class="hideEditView?'hidden':''">
                <td><input type="hidden" v-model="editUser.id"></td>
                <td><input type="text" v-model="editUser.name"></td>
                <td><input type="text" v-model="editUser.username" readonly></td>
                <td><input type="password" v-model="editUser.password"></td>
                <td><input type="text" v-model="editUser.jobno"></td>
                <td><input type="text" v-model="editUser.position"></td>
                <td>
                    <button class="btn btn-success btn-sm" @click="updateUserById">保存更新</button>
                </td>
            </tr>
        </table>



      </div>
    </div>
  </div>
</div><!-- /container -->

</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/js/jquery-1.12.4.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://fastly.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.38/assets/js/ie10-viewport-bug-workaround.js"></script>
<script src="/js/vue2.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            users:[],
            userheadimg:'',
            editUser:{
                id:0,
                name:'',
                username:'',
                password:'',
                jobno:'',
                position:'',
                headimg:''
            }, //编辑用户
            hideEditView:true, //是否显示编辑视图
            islogin:{}//当前登录的用户
        },
        methods:{
            loadAllUser:function(){//查询全部
                var self = this;
                $.getJSON("/user/all",function (r) {
                    console.log(r);
                    self.users = r.data;
                })
            },
            deleteUserById:function(id,index) {//根据id删除
                var self = this;
                $.getJSON("/user/delete/"+id, function(r){
                    self.users.splice(index,1);
                })
            },
            findByUserName:function() {//根据姓名查找
                var self = this;
                var value = $("#user").val();
                $.getJSON("/user/findByUserName/" + value, function(r){
                    console.log(r);
                    self.users = r.data;
                })
            },
            login:function() {//登录
                $.ajax({
                    url:'/login/doLogin',
                    type:'POST',
                    data: JSON.stringify(this.editUser , this.editUser), //转换为JSON
                    contentType:'application/json;charset=utf-8',
                    dataType:'json',
                    success:(r) => {
                        this.hideEditView = true;//不隐藏
                        app.loginD();
                        console.log(r);
                    },
                    error:()=> {
                        alert('登录失败');
                    }
                })
            },
            addUserByDefault:function () {//添加
                $.ajax({
                    url:'/user/save',
                    type:'POST',
                    data: JSON.stringify(this.editUser), //转换为JSON
                    contentType:'application/json;charset=utf-8',
                    dataType:'json',
                    success:(r) => {
                        // this.hideEditView = true;//不隐藏
                        this.users.push(r.data);//添加到双向绑定的用户数组中
                        console.log(r);
                    },
                    error:()=> {
                        alert('添加失败');
                    }
                })
                // $.post("/user/save", this.editUser,(r)=>{ //箭头函数，省略self中间变量
                // })
            },
            editUserById:function (id,idx) {//编辑
                this.hideEditView = !this.hideEditView;//不隐藏
                this.editUser = this.users[idx];
                delete this.editUser.storyList;
                delete this.editUser.taskList;
            },
            updateUserById:function () {//修改
                $.ajax({
                    url:'/user/update',
                    type:'POST',
                    data: JSON.stringify(this.editUser), //转换为JSON
                    contentType:'application/json;charset=utf-8',
                    dataType:'json',
                    success:(r) => {
                        this.hideEditView = true;//不隐藏
                        console.log(r.data);
                    },
                    error:()=> {
                        alert('修改失败');
                    }
                })
                // $.post("/user/update", this.editUser, (r)=>{ //箭头函数，省略self中间变量
                //     this.hideEditView = true;//不隐藏
                // })
            },
            loginD:function(){//查询当前登录用户
                var self = this;
                $.getJSON("/user/currentUser",function (r) {
                    console.log(r);
                    if (r.result){
                        self.islogin = r.data;//从Msg中取出当前登录的用户
                        self.islogin.headimg ="http://localhost:8080//"+self.islogin.headimg;
                        app.loadAllUser();
                    }
                })
            },
            loginT:function(){//退出登录
                var self = this;
                $.getJSON("/login/logout",function (r) {
                    self.user = null;
                    app.loginD();
                })
            },
            headimgT:function () {
                var self = this;
                $.getJSON("/user/findUserName/" + this.islogin.username,function (r) {
                    console.log(r);
                    if (r.result){
                        self.islogin = r.data;//从Msg中取出当前登录的用户
                        self.islogin.headimg ="http://localhost:8080//"+self.islogin.headimg;
                    }
                })
            }
        }
    })
    app.loginD();//先判断用户是否登录
</script>
</body>
</html>
